package com.seek.eduorder.service.impl;

import com.seek.commonutils.ordervo.CourseInfoOrder;
import com.seek.commonutils.ordervo.UcenterMemberInfoOrder;
import com.seek.eduorder.client.CourseClient;
import com.seek.eduorder.client.UcenterClient;
import com.seek.eduorder.entity.Order;
import com.seek.eduorder.mapper.OrderMapper;
import com.seek.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seek.servicebase.exceptionhandler.SeekException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wml
 * @since 2022-03-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UcenterClient ucenterClient;

    // 生成订单的方法
    @Override
    public String createOrder(String courseId, String memberId) {
        if (memberId == null || memberId.equals("")) {
            throw new SeekException(20001, "请登录！");
        }
        // 通过远程调用根据用户 id 获取用户信息
        UcenterMemberInfoOrder userInfoForOrder = ucenterClient.getUserInfoOrder(memberId);
        // 通过远程调用根据课程 id 获取课程信息
        CourseInfoOrder courseInfoOrder = courseClient.getCourseInfoOrder(courseId);

        Order order = new Order();
        order.setOrderNo(UUID.randomUUID().toString()); // 订单编号，不是订单 id
        order.setCourseId(courseId); // 课程 id
        order.setCourseTitle(courseInfoOrder.getTitle()); // 课程标题
        order.setCourseCover(courseInfoOrder.getCover()); // 课程封面
        order.setTeacherName(courseInfoOrder.getTeacherName()); // 讲师姓名
        order.setTotalFee(courseInfoOrder.getPrice()); // 课程价格
        order.setMemberId(memberId); // 会员 id
        order.setMobile(userInfoForOrder.getMobile()); // 会员手机号
        order.setNickname(userInfoForOrder.getNickname()); // 会员昵称
        order.setStatus(0); // 支付状态（默认0未支付，1支付）
        order.setPayType(2); // 支付方式（默认2支付宝）
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
